package api

import (
	"context"
	"fmt"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/common"
	"micro-grpc-server/app/http/controllers"
	pb "micro-grpc-server/services/music/proto"
)

type MusicController struct {
	controllers.Controller
	MusicClient pb.MusicService
}

func (c *MusicController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("GET", "/list", "List")
	b.Handle("GET", "/get-play-info", "PlayInfo")
	b.Handle("GET", "/get-lyric", "Lyric")
}

// List
// @Router /music/list [get]
// @Summary 歌曲列表
// @Description 歌曲列表
// @Tags Music
// @Produce json
// @Param query query music.ListMusicRequest false "搜索的数据"
// @Success 200 {array} music.Music
func (c *MusicController) List(ctx context.Context) {
	req := &pb.ListMusicRequest{}
	if err := c.ReadQuery(req); err != nil {
		c.BadRequest(err.Error())
		return
	}
	res, err := c.MusicClient.MusicSearch(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败 -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSucMeta(res.List, common.Extra{
		Count: res.Total,
		Page:  c.QueryPage(),
	})
}

// PlayInfo
// @Router /music/get-play-info [get]
// @Summary 歌曲列表
// @Description 歌曲列表
// @Tags Music
// @Produce json
// @Param query query music.PLayInfoRequest false "搜索的数据"
// @Success 200 object music.PlayInfoResponse
func (c *MusicController) PlayInfo(ctx context.Context) {
	req := &pb.PLayInfoRequest{}
	if err := c.ReadQuery(req); err != nil {
		c.BadRequest(err.Error())
		return
	}
	res, err := c.MusicClient.GetPlayInfo(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败 -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSuc(res)
}

// Lyric
// @Router /music/get-lyric [get]
// @Summary 获取歌词
// @Description 获取歌词
// @Tags Music
// @Produce json
// @Param query query music.LyricRequest false "搜索的数据"
// @Success 200 object music.LyricResponse
func (c *MusicController) Lyric(ctx context.Context) {
	req := &pb.LyricRequest{}
	if err := c.ReadQuery(req); err != nil {
		c.BadRequest(err.Error())
		return
	}
	res, err := c.MusicClient.GetLyric(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败 -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.JsonSuc(res)
}
